package com.dhecp.project.oa.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.oa.domain.Stamp;
import com.dhecp.project.oa.mapper.StampMapper;
import com.dhecp.project.oa.service.StampService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 印章登记表 业务实现层
 * @author DH-lwl
 * @version 2020年7月8日
 */
@Service
public class StampServiceImpl implements StampService {

	@Autowired
	private StampMapper stampMapper;

	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;
	
	/**
	 * 查询所有数据
	 * @param params
	 * @return List-Stamp
	 * @author lwl
	 * @throws Exception 
	 */
	@Override
	public List<Stamp> queryAll(Map<String, Object> params) throws Exception{
		params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
		params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));

        params.put(DataFiltering.TABLE_ALIAS, "J1");	//数据权限表别名
		params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));

		if (params.get("field") == null || params.get("field") == ""){
			String orderBy = "createTime asc";
			PageUtils.startPageOrder(orderBy);
		}else{
			PageUtils.startPage();	
		}	
		
		return stampMapper.findAll(params);
	}
	
	/**
	 * 通过guid查询单条记录
	 * @param guid
	 * @return 实体Stamp
	 * @author lwl
	 */
	@Override
	public Stamp queryByGuid(String guid) {
		return stampMapper.findByGuid(guid);
	}
	
	/**
	 * 新增数据
	 * @param stampItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult insertStamp(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<Stamp> stampList = JSON.parseArray(params.get("stampItems").toString(), Stamp.class);
		if (stampList == null || stampList.size() == 0) {
			return AjaxResult.error("请添加需要保存的信息。");
		}
		
		for (Stamp stamp : stampList) {
			stamp.setCreateUser(ShiroUtils.getUserId());
			stamp.setCreateTime(new Date());
			stampMapper.insertStamp(stamp);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 修改数据
	 * @param fixedAssetsItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult updateStamp(Map<String, Object> params) {
		List<Stamp> stampList = JSON.parseArray(params.get("stampItems").toString(), Stamp.class);
		if (stampList == null || stampList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
		
		for (Stamp stamp : stampList) {
			stamp.setUpdateUser(ShiroUtils.getUserId());
			stamp.setUpdateTime(new Date());			
			stampMapper.updateStamp(stamp);
		}
		return AjaxResult.success("操作成功！");
	}

	/**
	 * 通过Guid删除数据
	 * @param stampItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult deleteByGuid(Map<String, Object> params) {
		List<Stamp> stampList = JSON.parseArray(params.get("stampItems").toString(), Stamp.class);
		if (stampList == null || stampList.size() == 0) {
			return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
		}
		for (Stamp stamp : stampList) {
			stampMapper.deleteByGuid(stamp);
		}
		return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
	}

}
